﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoLiquidacion : Form
    {
        [Dependency]
        public ILiquidacionBL liquidacionBL { get; set; }
        [Dependency]
        public IAreaLiquidacionBL areaLiqBL { get; set; }
        [Dependency]
        public IContratoBL contratoBL { get; set; }
        [Dependency]
        public ITrabajadorBL trabBL { get; set; }

        public FrmMttoLiquidacion()
        {
            InitializeComponent();
            this.BuildUp();
        }
        bool isNew;

        public void NuevaLiquidacion()
        {
            isNew = true;
            lIQUIDACIONBindingSource.AddNew();
        }

        public LIQUIDACION _liquidacion { get; set; }

        public void EditarLiquidacion()
        {
            isNew = false;
            lIQUIDACIONBindingSource.DataSource = _liquidacion;
            pERSONABindingSource.DataSource = _liquidacion.CONTRATO.TRABAJADOR.PERSONA;
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var liquidacion = lIQUIDACIONBindingSource.Current as LIQUIDACION;

            var msg = "ERROR";

            if (liquidacion != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        ////liquidacion.AREA_LIQUIDACION.Descripcion = descripcionTextBox.Text.ToUpper();
                        liquidacionBL.AddLiquidacion(liquidacion);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        ////liquidacion.AREA_LIQUIDACION.Descripcion = descripcionTextBox.Text.ToUpper();
                        liquidacionBL.UpdateLiquidacion(liquidacion);
                        MessageBox.Show("Modificación Correcta", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btnAreaLiq_Click(object sender, EventArgs e)
        {
            FrmAreaLiquidacion frm = new FrmAreaLiquidacion();
            frm.Extend();
            frm.ShowDialog();

            if (frm.CurrentArea != null)
            {
               var nuevaArea = areaLiqBL.GetFromAreaById(frm.CurrentArea.IdAreaLiquidacion);
               var liquidacion = lIQUIDACIONBindingSource.Current as LIQUIDACION;
              // liquidacion.AREA_LIQUIDACION = nuevaArea;
              // liquidacion.IdAreaLiquidacion = nuevaArea.IdAreaLiquidacion;
               lIQUIDACIONBindingSource.ResetBindings(true);
            }
        }

        private void btnContrato_Click(object sender, EventArgs e)
        {
            FrmContrato frm = new FrmContrato();
            frm.Extend();
            frm.ShowDialog();
            
            if (frm.currentContrato != null)
            {
                var nuevoContrato = contratoBL.GetFromContratoById(frm.currentContrato.IdContrato);
                var nuevoTrab = trabBL.GetFromTrabajadorById(frm.currentContrato.IdTrabajador);

                var liquidacion = lIQUIDACIONBindingSource.Current as LIQUIDACION;
                pERSONABindingSource.DataSource = nuevoTrab.PERSONA;
                liquidacion.CONTRATO = nuevoContrato;
                liquidacion.IdContrato = nuevoContrato.IdContrato;
                lIQUIDACIONBindingSource.ResetBindings(true);
            }
        }

        private void FrmMttoLiquidacion_Load(object sender, EventArgs e)
        {
            //lIQUIDACIONBindingSource.AddNew();
        }

        private void observacionesTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnAddEspecialidad_Click(object sender, EventArgs e)
        {
            var _liquidacion = lIQUIDACIONBindingSource.Current as LIQUIDACION;
            AREA_LIQUIDACION CurrentArea = lIQUIDACION_AREA_LIQUIDACIONBindingSource.Current as AREA_LIQUIDACION;
            FrmAreaLiquidacion frm = new FrmAreaLiquidacion();
            frm.Extend();
            frm.ShowDialog();
            if (frm.CurrentArea != null)
            {
                CurrentArea = frm.CurrentArea;

                var _areaLiq = new LIQUIDACION_AREA_LIQUIDACION
                {
                    IdAreaLiquidacion = CurrentArea.IdAreaLiquidacion,
                    AREA_LIQUIDACION = CurrentArea
                };

                var existe = _liquidacion.LIQUIDACION_AREA_LIQUIDACION.Where(p => p.AREA_LIQUIDACION.IdAreaLiquidacion.Equals(_areaLiq.IdAreaLiquidacion)).SingleOrDefault();
                if (existe == null)
                {
                    lIQUIDACION_AREA_LIQUIDACIONBindingSource.Add(_areaLiq);
                    lIQUIDACION_AREA_LIQUIDACIONBindingSource.ResetBindings(true);
                }
                else
                {
                    var rpt = MessageBox.Show(this, string.Format("La Especialidad: {0}, " + "ya se agregó", CurrentArea.Descripcion),
                                       "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void descripcionTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
